shell script

您所在的位置:网站首页 netcat shell跳板 shell script

shell script

#shell script| 来源: 网络整理| 查看: 265

Because of the documentation :The -w flag has no effect on the -l option, i.e. nc will listen forever for a connection, with or without the -w flag

I tried nc ncat socat, no one can set timeout for server mode.

As far as I know, only busybox nc can follow -w option in server mode.

So you have to download busybox which compiled with CONFIG_NC_SERVER=y option, or compile it by your self.

So that you can

$busybox nc -w 10 -l -p 9999

But my system's busybox didn't compile with CONFIG_NC_SERVER=y option, and I don't want compile it. So I use this solution:

portnum=9999 (sleep 10 ;echo "T" | nc -w 1 127.0.0.1 $portnum) | nc -N -l -p $portnum

After 10 seconds, send "T" to localhost:9999

The full code is

#!/bin/sh portnum=9999 testmsg="Hello_World" if [ "$( (sleep 10 ;echo "T" | nc -w 1 127.0.0.1 $portnum) | nc -N -l -p $portnum )" = $testmsg ]; then echo "Test pass" else echo "Test not pass" fi

If server received Hello_World in 10 seconds, it will print Test pass. Otherwise print Test not pass

You can try open another console, and type

echo "Hello_World" | nc -w 1 127.0.0.1 9999

to test it.



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3